<?php
/**
 * Created by PhpStorm.
 * User: tsy
 * Date: 2017/6/1
 * Time: 15:13
 */
namespace Admin\Controller\Customer;
use Think\Controller;
use Admin\Controller\Common\BaseController;
use Admin\Controller\Common\VPNController;

class CustCreditController extends BaseController
{
    public function borrow_detail_credit(){
        $cust_id=I("post.cust_id");

        $ajax_array=array();
        $zmxy_array1=array();
        $zmxy_array2=array();

        $custcredit=D("CustCredit");
        $zmxy_score=$custcredit->select_zmxy_score($cust_id);
        $shzx_score=$custcredit->select_shzx_score($cust_id);

        $zmxy_count=count($zmxy_score);
        if($zmxy_count==1){
            $zmxy_array1=$zmxy_score[0];
            $zmxy_array1['count']=1;
            $zmxy_array2['count']=0;
        }else if($zmxy_count==2){
            $zmxy_array1=$zmxy_score[0];
            $zmxy_array2=$zmxy_score[1];
            $zmxy_array1['count']=1;
            $zmxy_array2['count']=1;
        }else{
            $zmxy_array1['count']=0;
            $zmxy_array2['count']=0;
        }

        $ajax_array["zmxy_array1"]=$zmxy_array1;
        $ajax_array["zmxy_array2"]=$zmxy_array2;
        $ajax_array["shzx_array"]=$shzx_score;

        $this->ajaxReturn($ajax_array);
    }

    public function select_fm_table(){
        if(IS_POST&&I("post.borrow_id",'','/^\d+$/')!=''){
            $borrow_id=I("post.borrow_id");
            $CustCredit=D("CustCredit");
            $fm_table=$CustCredit->select_fm_table($borrow_id);
            $this->ajaxReturn(json_encode($fm_table));
        }
    }

    public function select_zmxy_table(){
        $cust_id=I("post.cust_id");
        $num=I("post.num");

        $CustCredit=D("CustCredit");
        $zxmy_table=$CustCredit->select_zmxy_table($cust_id,$num);
        $this->ajaxReturn(json_encode($zxmy_table));
    }

    public function curl_select_zmxy(){
        $cust_id=I("post.cust_id");

        $cust_info=D("Custinfo");
        $cust_array=$cust_info->select_cust_detail($cust_id);
        vendor("zhima.zhima_infoCheck");
        $zhima_infoCheck=new \zhima_infoCheck();
        $zhima_result=$zhima_infoCheck->ZhiMaInfo($cust_id, $cust_array['cust_identity'], $cust_array['cust_name'], $cust_array["cust_phone"], $cust_array['cust_email'],
            $cust_array['bc_bankcard'], $cust_array['cust_login_ip']);
        if ($zhima_result['success'] == 1 || $zhima_result['success'] == true)
        {
            $cust_credit=D("CustCredit");
            $num=$cust_credit->select_max_num($cust_id);
            $num++;
            $cust_credit->insert_zhima_score($cust_id,$zhima_result['ivs_score'],$num);
            $cust_credit->insert_zhima_rules($zhima_result['ivs_detail'],$cust_id,$num);
        }
//        $this->ajaxReturn($zhima_result);
    }

    public function delete_zmxy_score(){
        $cust_id=I("post.cust_id");
        $num=I("post.num");

        $cust_credit=D("CustCredit");
        $score_result=$cust_credit->delete_zmxy_score($cust_id,$num);
        $rule_result = $cust_credit->delete_zmxy_rule($cust_id, $num);

        if($score_result!=0&&$rule_result!=0){
            $result['state']=1;
            $result['message']="操作成功！";
        }else{
            $result['state']=2;
            $result['message']="操作失败！";
        }
        $this->ajaxReturn($result);

    }

    public function select_vpn(){
        $cust_id=I("post.cust_id");

        $cust_credit=D("CustCredit");
        $cust_info=D("Custinfo");
        $cust_array=$cust_info->get_baisc_by_id($cust_id);

        $VPN=new VPNController();
        $cookie=$VPN->VPN_login();
        $content=$VPN->queryCredit($cust_array[0]['cust_identity'],$cust_array[0]['cust_name']);
        $dom = new \DOMDocument();
        $dom->loadXML($content);
        $array=$VPN->getArray($dom->documentElement);
        $result=$array["soap:body"]["ns0:querycreditresponse"]["return"]["#text"];
        $result=str_replace("<","&lt;",$result);
        $result=str_replace(">","&gt;",$result);
        $json=json_decode($result);
        $state=$json[0]->state;
        $relation_path="";
        if($state==1) {
            $xml = $json[0]->result;
            $xml = str_replace("GBK", "UTF-8", $xml);
            $xml = str_replace("&lt;", "<", $xml);
            $xml = str_replace("&gt;", ">", $xml);
            $relation_path=$cust_id.".xml";
//            $path = "/var/www/html/Public/Upload/UploadVPN/" .$relation_path;
            $path=C("vpn_address").$relation_path;
            $myfile = fopen($path, "w");
            fwrite($myfile, $xml);
            fclose($myfile);
            $cust_credit->insert_cust_vpn($cust_id,$relation_path,$state);
            $array2['state']=1;
            $array2['message']="查询成功";
        }else if($state==2){
            $cust_credit->insert_cust_vpn($cust_id,$relation_path,$state);
            $array2['state']=2;
            $array2['message']="被查询人不存在";
        }else if($state==3){
            $array2['state']=2;
            $array2['message']="超过查询限制";

        }else if($state==4){
            $array2['state']=2;
            $array2['message']="其他错误";
        }
        $this->ajaxReturn($array2);
    }

    public function anrong_createapply(){
        $borrow_id=I("post.borrow_id");

        $ar_api=new \Admin\Controller\Common\AnRongApiController();

        $borrow_info=D("Borrowinfo");
        $borrow_info_array=$borrow_info->borrow_cust_info($borrow_id);
        $customerName=$borrow_info_array['cust_name'];
        $paperNumber=$borrow_info_array['cust_identity'];
        $applyId=$borrow_id;//申请id
        $loanType="02";//贷款类型 消费
        $loanMoney=$borrow_info_array['borrow_money']==''?100:$borrow_info_array['borrow_money'];
        $currency="1";//币种
        $loanTimeLimit=6;//期数
        $assureType="D";//担保类型 信用
        $applyDate=date("Y-m-d",time());//申请时间
        $creditAddress="长沙市";
        $result=$ar_api->createApply($customerName,$paperNumber,$applyId,$loanType,$loanMoney,$currency,$loanTimeLimit,$assureType,$applyDate,$creditAddress);
        if($result['state']==1){
            $an_rong=D("AnRong");
            $an_rong->insert_credit_score($borrow_id,$paperNumber);

            $borrow_info->update_borrow_first_examine($borrow_id);

        }
        $this->ajaxReturn($result);
    }
}